package com.clinic.action.prescription;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.clinic.action.common.AuthenticationAction;
import com.clinic.entity.gui.GuiDrug;
import com.clinic.entity.gui.GuiPagination;
import com.clinic.entity.gui.GuiPatient;
import com.clinic.entity.gui.GuiPrescription;
import com.clinic.entity.gui.GuiSyptom;
import com.clinic.entity.param.Index;
import com.clinic.entity.type.SortProperty;
import com.clinic.entity.type.TreatmentTime;
import com.clinic.logic.drug.DrugLogic;
import com.clinic.logic.prescription.PrescriptionLogic;
import com.clinic.logic.prescription.SyptomLogic;
import com.clinic.logic.user.PatientLogic;
import com.clinic.util.CommonUtil;

public class PrescriptionAction extends AuthenticationAction {

	private static final long serialVersionUID = -2775487674378981245L;
	
	@Autowired
	private PatientLogic patientLogic;
	@Autowired
	private SyptomLogic syptomLogic;
	@Autowired
	private PrescriptionLogic prescriptionLogic;
	@Autowired
	private DrugLogic drugLogic;
	
	private long id;
	private int page = 1;
	private String term;
	private SortProperty sort;
	private long patientId;
	private GuiPatient guiPatient;
	private List<GuiPatient> guiPatientList;
	private long rootPrescriptionId;
	private GuiPrescription guiPrescription;
	private List<GuiPrescription> guiPrescriptionList;
	private GuiPagination guiPagination;
	private List<GuiSyptom> guiSyptomList;
	private List<GuiDrug> guiDrugList;
	
	public String registration() {
		if(isDoctorLogin() || isAssistantLogin()) {
			if (patientId > 0) {
				// new prescription for patient
				guiPatient = patientLogic.getPatientDetailsById(patientId);
				guiPrescription = prescriptionLogic.getNewPrescriptionByPatient(guiPatient);
			} else if (rootPrescriptionId > 0) {
				// continue existing prescription
				guiPrescription = prescriptionLogic.getNewPrescriptionByRootPrescriptionId(rootPrescriptionId);
				guiPatient = guiPrescription.getPatient();
			} else {
				Index index = CommonUtil.getIndexBypage(page);
				if (StringUtils.isNotBlank(term) || sort != null) {
					guiPatientList = patientLogic.getPatientListByTerm(term, sort.getColumn(), index);
					guiPagination = patientLogic.getPaginationOfPatientsByTerm(term, sort.getColumn(), page);
				} else {
					guiPatientList = patientLogic.getPatients(index);
					guiPagination = patientLogic.getPaginationOfPatients(page);
				}
				for (GuiPatient guiPatient : guiPatientList) {
					guiPatient.setGuiPrescription(prescriptionLogic.getPastPrescriptionOfPatient(guiPatient.getUserId()));
				}
				return "select";
			}
			guiDrugList = drugLogic.getDrugList(new Index(1000));
			guiSyptomList = syptomLogic.getSyptomList(new Index(1000));
			return ActionResult.SUCCESS;
		}
		return ActionResult.AUTH_FAILED;
	}

	public String insert(){
		if(isDoctorLogin() || isAssistantLogin()) {
			guiPatient = patientLogic.getPatientDetailsById(guiPrescription.getPatient().getUserId());
			if (guiPatient.isAvailable()) {
				guiPrescription = prescriptionLogic.insertPrescription(guiPrescription);
				return ActionResult.SUCCESS;
			}
			return ActionResult.ERROR;
		}
		return ActionResult.AUTH_FAILED;
	}
	
	public String show(){
		if(isDoctorLogin() || isAssistantLogin()) {
			guiPrescription = prescriptionLogic.getPrescriptionById(id);
			if (guiPrescription.isAvailable()) {
				guiPatient = guiPrescription.getPatient();
				return ActionResult.SUCCESS;
			}
			return ActionResult.ERROR;
		}
		return ActionResult.AUTH_FAILED;
	}

	public String edit(){
		if(isDoctorLogin() || isAssistantLogin()) {
			guiPrescription = prescriptionLogic.getPrescriptionById(id);
			if (guiPrescription.isAvailable()) {
				guiPatient = guiPrescription.getPatient();
				guiDrugList = drugLogic.getDrugList(new Index(1000));
				guiSyptomList = syptomLogic.getSyptomList(new Index());
				return ActionResult.SUCCESS;
			}
			return ActionResult.ERROR;
		}
		return ActionResult.AUTH_FAILED;
	}

	public String delete(){
		if(isDoctorLogin() || isAssistantLogin()) {
			return ActionResult.SUCCESS;
		}
		return ActionResult.AUTH_FAILED;
	}

	public String list(){
		if(isDoctorLogin() || isAssistantLogin()) {
			guiPatient = patientLogic.getPatientDetailsById(patientId);
			if (guiPatient.isAvailable()) {
				guiPrescriptionList = prescriptionLogic.getPrescriptionsByPatientId(patientId);
				return ActionResult.SUCCESS;
			}
			return ActionResult.ERROR;
		}
		return ActionResult.AUTH_FAILED;
	}
	
	public String update(){
		if(isDoctorLogin() || isAssistantLogin()) {
			guiPatient = patientLogic.getPatientDetailsById(guiPrescription.getPatient().getUserId());
			if (guiPatient.isAvailable()) {
				guiPrescription = prescriptionLogic.updatePrescription(guiPrescription);
				return ActionResult.SUCCESS;
			}
			return ActionResult.ERROR;
		}
		return ActionResult.AUTH_FAILED;
	}

	public TreatmentTime[] getTreatmentTimeList() {
		return TreatmentTime.values();
	}
	
	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public boolean isTermExist() {
		return StringUtils.isNotBlank(term);
	}

	public String getTerm() {
		return term;
	}

	public void setTerm(String term) {
		this.term = term;
	}

	public boolean isSortExist() {
		return (sort != null);
	}

	public SortProperty getSort() {
		return sort;
	}
	
	public void setSort(String sortProperty) {
		this.sort = SortProperty.getValue(sortProperty);
	}

	public long getPatientId() {
		return patientId;
	}

	public void setPatientId(long patientId) {
		this.patientId = patientId;
	}

	public GuiPatient getGuiPatient() {
		return guiPatient;
	}

	public void setGuiPatient(GuiPatient guiPatient) {
		this.guiPatient = guiPatient;
	}

	public List<GuiPatient> getGuiPatientList() {
		return guiPatientList;
	}

	public void setGuiPatientList(List<GuiPatient> guiPatientList) {
		this.guiPatientList = guiPatientList;
	}

	public long getRootPrescriptionId() {
		return rootPrescriptionId;
	}

	public void setRootPrescriptionId(long rootPrescriptionId) {
		this.rootPrescriptionId = rootPrescriptionId;
	}

	public GuiPrescription getGuiPrescription() {
		return guiPrescription;
	}

	public void setGuiPrescription(GuiPrescription guiPrescription) {
		this.guiPrescription = guiPrescription;
	}

	public List<GuiPrescription> getGuiPrescriptionList() {
		return guiPrescriptionList;
	}

	public void setGuiPrescriptionList(List<GuiPrescription> guiPrescriptionList) {
		this.guiPrescriptionList = guiPrescriptionList;
	}

	public GuiPagination getGuiPagination() {
		return guiPagination;
	}

	public void setGuiPagination(GuiPagination guiPagination) {
		this.guiPagination = guiPagination;
	}

	public List<GuiDrug> getGuiDrugList() {
		return guiDrugList;
	}

	public List<GuiSyptom> getGuiSyptomList() {
		return guiSyptomList;
	}

	public void setGuiSyptomList(List<GuiSyptom> guiSyptomList) {
		this.guiSyptomList = guiSyptomList;
	}

	public void setGuiDrugList(List<GuiDrug> guiDrugList) {
		this.guiDrugList = guiDrugList;
	}

}